#include "BSP_UART.h"

uint8_t UART1_Recv[UART_RECV_MAX];
uint8_t UART2_Recv[UART_RECV_MAX];
uint8_t UART3_Recv[UART_RECV_MAX];
uint16_t UART1_RecvBytes = 0;
uint16_t UART2_RecvBytes = 0;
uint16_t UART3_RecvBytes = 0;

STATUS UART1_Init(uint32_t BaudRate)
{
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    UART1_Clear();

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | 
                RCC_APB2Periph_GPIOA, ENABLE);

    USART_DeInit(USART1);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
        
    USART_InitStructure.USART_BaudRate = BaudRate;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl 
                    = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART1, &USART_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART1, ENABLE);
    return ok;
}
STATUS UART2_Init(uint32_t BaudRate)
{
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;


    UART2_Clear();

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    USART_DeInit(USART2);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
        
    USART_InitStructure.USART_BaudRate = BaudRate;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl 
                    = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART2, &USART_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART2, ENABLE);
    return ok;
}
STATUS UART3_Init(uint32_t BaudRate)
{
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    UART3_Clear();


    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    USART_DeInit(USART3);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
        
    USART_InitStructure.USART_BaudRate = BaudRate;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl 
                    = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART3, &USART_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART3, ENABLE);
    
    return ok;
}

STATUS UART1_SendByte(char ch)
{
    USART_SendData(USART1, ch);
    while(USART_GetFlagStatus(USART1, USART_FLAG_TC)!=SET);
    return ok;
}

STATUS UART2_SendByte(char ch)
{
    USART_SendData(USART2, ch);
    while(USART_GetFlagStatus(USART2, USART_FLAG_TC)!=SET);
    return ok;
}


STATUS UART3_SendByte(char ch)
{
    USART_SendData(USART3, ch);
    while(USART_GetFlagStatus(USART3, USART_FLAG_TC)!=SET);
    return ok;
}

void USART1_IRQHandler(void)
{
    if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET) //中断产生
    {
        USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清除中断标志
        if(UART1_RecvBytes>=UART_RECV_MAX) UART1_RecvBytes = 0;
        UART1_Recv[UART1_RecvBytes++] = USART_ReceiveData(USART1);
    }  
}

void USART2_IRQHandler(void)
{
    if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) //中断产生
    {
        USART_ClearITPendingBit(USART2,USART_IT_RXNE); //清除中断标志
        if(UART2_RecvBytes>=UART_RECV_MAX) UART2_RecvBytes = 0;
        UART3_Recv[UART2_RecvBytes++] = USART_ReceiveData(USART2);
    }  
}

void USART3_IRQHandler(void)
{
    if(USART_GetITStatus(USART3,USART_IT_RXNE) != RESET) //中断产生
    {
        USART_ClearITPendingBit(USART3,USART_IT_RXNE); //清除中断标志
        if(UART3_RecvBytes>=UART_RECV_MAX) 
					UART3_RecvBytes = 0;
        
				UART3_Recv[UART3_RecvBytes++] = USART_ReceiveData(USART3);
        
    }   
}

void UART1_Clear(void)
{
    memset(UART1_Recv, 0, UART_RECV_MAX);
    UART1_RecvBytes = 0;
}

void UART2_Clear(void)
{
    memset(UART2_Recv, 0, UART_RECV_MAX);
    UART2_RecvBytes = 0;
}

void UART3_Clear(void)
{
    memset(UART3_Recv, 0, UART_RECV_MAX);
    UART3_RecvBytes = 0;
}

STATUS UART1_GetData(uint8_t *buf, uint16_t *len)
{
    if(buf == NULL) return error;
    memcpy(buf, UART1_Recv, UART1_RecvBytes);
    *len = UART1_RecvBytes;
    UART1_Clear();
    return ok;
}

STATUS UART2_GetData(uint8_t *buf, uint16_t *len)
{
    if(buf == NULL) return error;
    memcpy(buf, UART2_Recv, UART2_RecvBytes);
    *len = UART2_RecvBytes;
    UART2_Clear();
    return ok;
}

STATUS UART3_GetData(uint8_t *buf, uint16_t *len)
{
    if(buf == NULL) return error;
    memcpy(buf, UART3_Recv, UART3_RecvBytes);
    *len = UART3_RecvBytes;
    UART3_Clear();
    return ok;
}

STATUS UART1_SendStr(const int8_t * str)
{
    uint16_t p=0;
    if(str==NULL) return error;
    while(str[p]!='\0')
    {
        UART1_SendByte(str[p++]);
    }
    return ok;
}
STATUS UART2_SendStr(const int8_t * str)
{
    uint16_t p=0;
    if(str==NULL) return error;
    while(str[p]!='\0')
    {
        UART2_SendByte(str[p++]);
    }
    return ok;
}
STATUS UART3_SendStr(const int8_t * str)
{
    uint16_t p=0;
    if(str==NULL) return error;
    while(str[p]!='\0')
    {
        UART3_SendByte(str[p++]);
    }
    return ok;
}

STATUS UART1_SendData(const uint8_t *buf, uint16_t bytes)
{
    uint16_t p=0;
    if(buf==NULL) return error;
    while(p<bytes)
    {
        UART1_SendByte(buf[p++]);
    }
    return ok;
}

STATUS UART2_SendData(const uint8_t *buf, uint16_t bytes)
{
    uint16_t p=0;
    if(buf==NULL) return error;
    while(p<bytes)
    {
        UART2_SendByte(buf[p++]);
    }
    return ok;
}

STATUS UART3_SendData(const uint8_t *buf, uint16_t bytes)
{
    uint16_t p=0;
    if(buf==NULL) return error;
    while(p<bytes)
    {
        UART3_SendByte(buf[p++]);
    }
    return ok;
}
